﻿using System;

using System.Collections.Generic;
using System.Text;
using Fluid.Controls;
using System.Drawing;
using System.Windows.Forms;
using PasswordSafe.Xml;
using PasswordSafe.Classes;

namespace PasswordSafe.Templates
{
    public class DetailTemplate : FluidTemplate
    {
        protected override void InitControl()
        {
            base.InitControl();
            Editable = true;

            this.Bounds = new Rectangle(0, 0, 240, 40);
            FluidLabel l;

            FluidTextBox tb = new FluidTextBox("", 3, 17, 228, 24);
            tb.LineAlignment = StringAlignment.Near;
            tb.Alignment = StringAlignment.Near;
            tb.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
            tb.Font = new Font(FontFamily.GenericSansSerif, 10f, FontStyle.Bold);
            tb.ShowBorder = false;
            tb.BackColor = Color.Transparent;
            tb.TextChanged += new EventHandler(tb_TextChanged);
            tb.FocusChanged += new EventHandler(tb_FocusChanged);
            tb.Bind += new EventHandler(tb_Bind);
            valueTb = tb;
            Controls.Add(tb);

            l = new FluidLabel("", 3, 3, 228, 14);
            l.LineAlignment = StringAlignment.Near;
            l.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
            l.Font = new Font(FontFamily.GenericSansSerif, 7f, FontStyle.Regular);
            l.ForeColor = Theme.Current.ListSecondaryForeColor;
            nameLabel = l;
            Controls.Add(l);
        }

        void tb_FocusChanged(object sender, EventArgs e)
        {
            EnsureBound();
        }

        private void EnsureBound()
        {
            if (ItemIndex != SelectedItemIndex)
            {
                Bind(SelectedItemIndex);
            }
        }

        void tb_Bind(object sender, EventArgs e)
        {
            if (ItemIndex != SelectedItemIndex)
            {
                BindData();
            }
        }

        public int SelectedItemIndex { get { return (Parent as FluidListBox).SelectedItemIndex; } }


        void tb_TextChanged(object sender, EventArgs e)
        {
           // EnsureBound();
            PWNameValue item = this.Item as PWNameValue;
            if (item != null)
            {
                item.Value = valueTb.Text;
            }
        }

        private FluidLabel nameLabel;
        private FluidTextBox valueTb;

        public bool Editable { get; set; }

        protected override void OnBindValue(object value)
        {
            PWNameValue item = value as PWNameValue;
            if (item != null)
            {
                nameLabel.Text = item.Title;
                valueTb.Text = item.Value;
            }
            else
            {
                nameLabel.Text = "";
                valueTb.Text = "";
            }
        }

        public DetailListBox DetailListBox { get { return Parent as DetailListBox; } }

        protected override void OnItemUpdate(object value)
        {
            DetailListBox lb = DetailListBox;
            bool isSelected = lb.SelectedItemIndex == this.ItemIndex;
            nameLabel.ForeColor = isSelected ? Theme.Current.ListSecondarySelectedForeColor : Theme.Current.ListSecondaryForeColor;
            valueTb.AllowEdit = Editable && (SelectedItemIndex == ItemIndex);
        }

        public void BindData()
        {
            Bind((Parent as DetailListBox).SelectedItemIndex);
        }

        public override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (!valueTb.Focused)
                    {
                        valueTb.Focus();
                    }
                    else Parent.Focus();
                    e.Handled = true;

                    break;
            }
        }


        public void InsertKey(KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r') return;
            if (ItemIndex != SelectedItemIndex)
            {
                BindData();
            }
            if (!valueTb.Focused)
            {
                Focus();
                if (valueTb.Focused) valueTb.OnKeyPress(e);
            }
        }

        public bool IsFocused { get { return valueTb.Focused; } }

        public bool GetFocus { get; set; }

        public override void Focus()
        {
            if (!valueTb.Focused)
            {
                valueTb.Focus();
                valueTb.SelectAll();
            }
        }
    }
}
